package com.focus.xyz.union.pages.frontend;

import com.focus.xyz.core.common.keyword.Action;
import com.focus.xyz.core.pages.BasePage;
import com.focus.xyz.core.utils.Constants;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.PageFactory;

import java.util.List;

/**
 * Created by chenmeng on 2016/5/11.
 */
public class DestinationPage extends BasePage {
    private Action action;

    @FindBy(id = "dev_keyword")
    private WebElement keyword;
    @FindBy(id = "dev_prodSearchBtn")
    private WebElement searchBtn;
    /*搜索结果区域*/
    @FindBy(id = "div_PopFrame_divNodes")
    private WebElement result;
    @FindBy(id = "div_PopFrame_submit")
    private WebElement confirmBtn;

    @FindBy(id = "div_PopFrame")
    private WebElement destDiv;
    public DestinationPage(WebDriver driver, Action action) {
        super(driver, action);
        this.action = action;
        PageFactory.initElements(driver,this);
    }


    public void inputDestination(String key) throws Exception{
        action.inputText(keyword,key);
    }

    public void searchDest() throws Exception{
        action.click(searchBtn);
    }

    public void selectDest(String key) throws Exception{
        List<WebElement> destList = result.findElements(By.xpath("li/input"));
        if(destList.size()>0){
            for(WebElement e:destList){
                if(e.getAttribute(Constants.ATTR_VALUE).contains(key)){
                    action.click(e);
                    break;
                }
            }
        }
    }

    public void clickSubmitBtn() throws Exception{
        action.click(confirmBtn);
    }

    public void selectDestination(String key) throws Exception{
        inputDestination(key);
        searchDest();
        selectDest(key);
        clickSubmitBtn();
    }

    public void popDestDiv() throws Exception{
        String style = "position: fixed; top: 50%; left: 50%; margin-top: -267px; margin-left: -313px; z-index: 10001; display: block;";
        if(destDiv.getAttribute("style").contains("display: none")){
            action.setAttribute(destDiv,"style",style);
        }
    }


}
